package com.xy.biz.base.domain.converter;

import com.xy.biz.base.domain.bo.BomBO;
import com.xy.biz.base.domain.po.BomPO;
import com.xy.biz.base.domain.query.BomByIdQuery;
import com.xy.biz.base.domain.query.BomQuery;
import com.xy.biz.base.domain.req.GetBomRequest;
import com.xy.biz.base.domain.req.ListBomRequest;
import com.xy.biz.base.domain.req.SaveBomRequest;
import com.xy.biz.base.domain.resp.GetBomResponse;
import com.xy.tool.util.BeanUtilsEx;

import java.util.Collection;
import java.util.List;

/**
 *
 *
 * @author zsp
 * @date 2021-12-28
 */
public class BomConverter {

    public static BomPO toPO(BomBO source) {
        return BeanUtilsEx.copyProperties(source, () -> new BomPO(), true);
    }

    public static List<BomPO> toPO(Collection<BomBO> source) {
        return BeanUtilsEx.copyPropertiesForNewList(source, () -> new BomPO(), true);
    }

    public static BomBO fromPO(BomPO source) {
        return BeanUtilsEx.copyProperties(source, () -> new BomBO());
    }

    public static List<BomBO> fromPO(Collection<BomPO> source) {
        return BeanUtilsEx.copyPropertiesForNewList(source, () -> new BomBO());
    }

    public static BomBO toBO(SaveBomRequest source) {
        return BeanUtilsEx.copyProperties(source, () -> new BomBO(),
                (s, t) -> {
                    t.setGoodsList(BomGoodsConverter.toBO(s.getGoodsList()));
                    t.setEquipmentList(BomEquipmentConverter.toBO(s.getEquipmentList()));
                });
    }

    public static GetBomResponse toResponse(BomBO source) {
        return BeanUtilsEx.copyProperties(source, () -> new GetBomResponse(),
                (s, t) -> {
                    t.setGoodsList(BomGoodsConverter.toResponse(s.getGoodsList()));
                    t.setEquipmentList(BomEquipmentConverter.toResponse(s.getEquipmentList()));
                });
    }

    public static List<GetBomResponse> toResponse(Collection<BomBO> source) {
        return BeanUtilsEx.copyPropertiesForNewList(source, () -> new GetBomResponse(),
                (s, t) -> {
                    t.setGoodsList(BomGoodsConverter.toResponse(s.getGoodsList()));
                    t.setEquipmentList(BomEquipmentConverter.toResponse(s.getEquipmentList()));
                });
    }

    public static BomQuery toQuery(ListBomRequest source) {
        if (null == source) {
            return null;
        }
        BomQuery target = new BomQuery(source.getPageIndex(), source.getPageSize());
        BeanUtilsEx.copyProperties(source, target);
        return target;
    }

    public static BomByIdQuery toQuery(GetBomRequest source) {
        if (null == source) {
            return null;
        }
        return BeanUtilsEx.copyProperties(source, BomByIdQuery::new);
    }

}
